package com.gw2.gw2world.controller;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import com.gw2.gw2world.R;
import com.gw2.gw2world.model.Event;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.PopupMenu;
import android.widget.PopupMenu.OnMenuItemClickListener;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class ActiveEventManager extends ArrayAdapter<List<Event> > implements OnLongClickListener, OnMenuItemClickListener, Serializable {
	private static final long serialVersionUID = 1L;
	@SuppressWarnings("unused")
	private static final String TAG="ActiveEventManager";
	public static final int BIT_GROUP=0x0100;
	private Map<String, List<Event> > events;
	private List<String> ignoredEvents, buggedEvents;
	private SharedPreferences prefs;
	SharedPreferences.Editor editor;
	private boolean guesting=false, states=false;
	private String selectedEvent="";
	private PopupMenu menu;
	public ActiveEventManager(Context ct) {
		super(ct, android.R.layout.simple_list_item_1, new ArrayList<List<Event> >(100));
		//setDropDownViewResource(R.layout.active_event_line);
		prefs = PreferenceManager.getDefaultSharedPreferences(ct);
		events=new LinkedHashMap<String, List<Event> >(100);
		ignoredEvents=new ArrayList<String>(10);
		buggedEvents =new ArrayList<String>(10);
		ignoredEvents.addAll(Arrays.asList(TextUtils.split(prefs.getString("ignored_events", ""),",")));
		buggedEvents .addAll(Arrays.asList(TextUtils.split(prefs.getString("bugged_events" , ""),",")));
	}
	public void clearAndAdd(Vector<Event> v) {
		events.clear();
		clear();
		for(Event e : v) {
			/*if(!states && (!e.state.equals("Active") || ignoredEvents.contains(e.id) || buggedEvents.contains(e.id)) || e.level<min_level || e.level>max_level) continue;
			if(!livingWorld && e.living_world) {
				events.remove(e.id);
			}*/
			if(events.get(e.id)!=null) events.get(e.id).add(e);
			else {
				List<Event> l=new ArrayList<Event>(3);
				l.add(e);
				events.put(e.id, l);
			}
		}
		for(List<Event> le : events.values()) add(le);
		notifyDataSetChanged();
	}
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if(convertView==null) {
			LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(R.layout.active_event_line, parent, false);
		}
		TextView tv_event = (TextView)convertView.findViewById(R.id.tv_ae_name);
		TextView tv_level = (TextView)convertView.findViewById(R.id.tv_ae_level);
		TextView tv_state_home  =(TextView)convertView.findViewById(R.id.tv_ae_state_home);
		TextView tv_state_guest1=(TextView)convertView.findViewById(R.id.tv_ae_state_guest1);
		TextView tv_state_guest2=(TextView)convertView.findViewById(R.id.tv_ae_state_guest2);
		tv_event.setText(getItem(position).get(0).name);
		tv_level.setText(String.valueOf(getItem(position).get(0).level));
		ImageView iv_group=(ImageView)convertView.findViewById(R.id.iv_ae_group);
		if(getItem(position).get(0).group) iv_group.setVisibility(View.VISIBLE);
		else iv_group.setVisibility(View.INVISIBLE);
		ImageView iv_home  =(ImageView)convertView.findViewById(R.id.iv_led_home);
		ImageView iv_guest1=(ImageView)convertView.findViewById(R.id.iv_led_guest1);
		ImageView iv_guest2=(ImageView)convertView.findViewById(R.id.iv_led_guest2);
		iv_home  .setVisibility(View.GONE);
		iv_guest1.setVisibility(View.GONE);
		iv_guest2.setVisibility(View.GONE);
		if(states) {
			tv_state_home  .setVisibility(View.VISIBLE);
			tv_state_guest1.setVisibility(View.VISIBLE);
			tv_state_guest2.setVisibility(View.VISIBLE);
		} else {
			tv_state_home  .setVisibility(View.GONE);
			tv_state_guest1.setVisibility(View.GONE);
			tv_state_guest2.setVisibility(View.GONE);
		}
		if(guesting) {
			iv_home  .setVisibility(View.VISIBLE);
			iv_guest1.setVisibility(View.VISIBLE);
			iv_guest2.setVisibility(View.VISIBLE);
			iv_home  .setImageResource(R.drawable.led_off);
			iv_guest1.setImageResource(R.drawable.led_off);
			iv_guest2.setImageResource(R.drawable.led_off);
			int activate=0;
			for(Event e : getItem(position)) { //light led for the matched server
				if(e.server_id==prefs.getInt("home_server", 0)) {
					if(e.isStrictlyActive()) activate+=1;
					tv_state_home  .setText(Character.toString(e.state.charAt(0)));
					tv_state_home  .setTextColor(Tools.getColorFromState(getContext(), e.state));
				}
				if(e.server_id==prefs.getInt("guest1_server", 0)) {
					if(e.isStrictlyActive()) activate+=2;
					tv_state_guest1.setText(Character.toString(e.state.charAt(0)));
					tv_state_guest1.setTextColor(Tools.getColorFromState(getContext(), e.state));
				}
				if(e.server_id==prefs.getInt("guest2_server", 0)) {
					if(e.isStrictlyActive()) activate+=4;
					tv_state_guest2.setText(Character.toString(e.state.charAt(0)));
					tv_state_guest2.setTextColor(Tools.getColorFromState(getContext(), e.state));
				}
			}
			if((activate&1)==1) iv_home  .setImageResource(R.drawable.led_on);
			if((activate&2)==2) iv_guest1.setImageResource(R.drawable.led_on);
			if((activate&4)==4) iv_guest2.setImageResource(R.drawable.led_on);
		} else {
			tv_state_home  .setText(Character.toString(getItem(position).get(0).state.charAt(0)));
			tv_state_home  .setTextColor(Tools.getColorFromState(getContext(), getItem(position).get(0).state));
		}
		RelativeLayout rl=(RelativeLayout)convertView.findViewById(R.id.rl_ae_line);
		rl.setOnLongClickListener(this);
		rl.setTag(getItem(position).get(0).id);
		return convertView;
	}
	public void setGuestingEnabled(boolean v) { guesting=v;}
	public void setStatesEnabled(boolean v) {states=v;}
	@Override
	public boolean onLongClick(View v) {
		menu=new PopupMenu(getContext(), v);
		menu.getMenuInflater().inflate(R.menu.popup_active_event, menu.getMenu());
		menu.setOnMenuItemClickListener(this);
		selectedEvent=(String)v.getTag();
		menu.show();
		return false;
	}
	@Override
	public boolean onMenuItemClick(MenuItem mi) {
		editor=prefs.edit();
		if(mi.getItemId()==R.id.mi_ae_disable) {
			ignoredEvents.add(selectedEvent);
			editor.putString("ignored_events", TextUtils.join(",", ignoredEvents));
		} else if(mi.getItemId()==R.id.mi_ae_bugged) {
			buggedEvents.add(selectedEvent);
			editor.putString("bugged_events", TextUtils.join(",", buggedEvents));
		}
		editor.commit();
		events.remove(selectedEvent);
		for(int i=0;i<getCount();i++)
			if(getItem(i).get(0).id.equals(selectedEvent)) remove(getItem(i));
		notifyDataSetChanged();
		return false;
	}
}
